package com.duoduo.admin.controller;

import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.duoduo.admin.api.dto.SysUserDTO;
import com.duoduo.admin.api.dto.UserInfo;
import com.duoduo.admin.service.SysUserService;
import com.duoduo.common.core.util.Page;
import com.duoduo.common.core.util.PageRequest;
import com.duoduo.common.core.util.R;
import com.duoduo.common.security.annotation.Inner;
import com.duoduo.common.security.util.SecurityUtils;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
@Tag(name = "用户管理模块")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysUserController {
	
	private final SysUserService userService;
	
	/**
	 * 获取当前用户全部信息
	 * @return 用户信息
	 */
	@GetMapping(value = { "/info" })
	public R<UserInfo> info() {
		String username = SecurityUtils.getUser().getUsername();
		return R.ok(userService.info(username));
	}

	
	/**
	 * 分页
	 * @param request
	 * @return
	 */
	@PostMapping("page")
	@Operation(summary = "分页")
	public R<Page<SysUserDTO>> page(@RequestBody PageRequest<SysUserDTO> request){
		Page<SysUserDTO> page = userService.page(request.getPage(),request.getPageSize(),request.getParam());
		return R.ok(page);
	}
	/**
	 * 详情
	 * @param id
	 * @return
	 */
	@GetMapping("{id}/get")
	@Operation(summary = "详情")
	public R<SysUserDTO> get(@Parameter(description = "主键id") @PathVariable("id")Long id){
		return R.ok(userService.get(id));
	}
	/**
	 * 创建
	 * @param id
	 * @return
	 */
	@Operation(summary = "创建")
	@PostMapping("create")
	public R<SysUserDTO> create(@RequestBody SysUserDTO request){
		userService.create(request);
		return R.ok();
	}
	/**
	 * 修改
	 * @param id
	 * @return
	 */
	@Operation(summary = "修改")
	@PutMapping("{id}/update")
	public R<SysUserDTO> update(@RequestBody SysUserDTO request){
		userService.update(request,request.getId());
		return R.ok();
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Operation(summary = "删除")
	@DeleteMapping("{id}/delete")
	public R<SysUserDTO> delete(@Parameter(description = "主键id")@PathVariable("id")Long id){
		userService.delete(id);
		return R.ok();
	}
	@Inner
	@GetMapping(value = "/info/{username}")
	public R<UserInfo> info(@Parameter(description = "用户名")@PathVariable("username") String username){
		return R.ok(userService.info(username));
	};
}
